package com.kstech.mp3604.manager

import android.content.Context
import android.media.MediaPlayer
import android.net.Uri
import androidx.core.net.toUri
import com.kstech.mp3604.R

/**
 * author: blankii
 * created on: 2025/5/20 10:34
 * description:播放一些循环警报
 * mediaplayer 使用文档https://blog.csdn.net/Jason_Lee155/article/details/126927661
 */
object SoundManager {
    private lateinit var player: MediaPlayer

    private var isSerialPlayering = false //是否应急、自动回正、自动回位在播报，播报的话不执行musicType 8
    fun start(context: Context, musicType: Int) {
        // 优先级 1-7 高于 8
        if (musicType == 8 && isSerialPlayering) {
            return
        }
        var uri: Uri? = null
        if (!this::player.isInitialized) {
            player = MediaPlayer()
        }
        when (musicType) {
            //应急1 自动回正0 自动回位0
            1 -> {
                isSerialPlayering = true
                uri = "android.resource://${context.packageName}/${R.raw.music_1_0_0}".toUri()
            }
            //应急0 自动回正1 自动回位0
            2 -> {
                isSerialPlayering = true
                uri = "android.resource://${context.packageName}/${R.raw.music_0_1_0}".toUri()
            }
            //应急0 自动回正0 自动回位1
            3 -> {
                isSerialPlayering = true
                uri = "android.resource://${context.packageName}/${R.raw.music_0_0_1}".toUri()
            }
            //应急1 自动回正1 自动回位0
            4 -> {
                isSerialPlayering = true
                uri = "android.resource://${context.packageName}/${R.raw.music_1_1_0}".toUri()
            }
            //应急1 自动回正0 自动回位1
            5 -> {
                isSerialPlayering = true
                uri = "android.resource://${context.packageName}/${R.raw.music_1_0_1}".toUri()
            }
            //应急0 自动回正1 自动回位1
            6 -> {
                isSerialPlayering = true
                uri = "android.resource://${context.packageName}/${R.raw.music_0_1_1}".toUri()
            }
            //应急1 自动回正1 自动回位1
            7 -> {
                isSerialPlayering = true
                uri = "android.resource://${context.packageName}/${R.raw.music_1_1_1}".toUri()
            }
            //注意到边距 防止碰撞
            8 -> {
                uri = "android.resource://${context.packageName}/${R.raw.music_2}".toUri()
            }
        }


        player.apply {
            this.reset()
            this.setDataSource(context, uri!!)
            this.prepare()
            this.setOnPreparedListener {
                it.start()
                it.isLooping = true
            }
        }
    }

    /**
     * “注意到边距 防止碰撞” 正在播放中时， isTasking 的切换可以关闭掉他
     * 防止误关其他的声音播放
     */
    fun stop(musicType: Int = -1) {
        if (musicType == 8 && isSerialPlayering) {
            return
        } else if (musicType != 8) {
            isSerialPlayering = false
        }
        if (this::player.isInitialized) {
            player.stop()
        }
    }

    fun close() {
        isSerialPlayering = false
        if (this::player.isInitialized) {
            player.release()
        }
    }
}